<template>
  <div class="channelView">
    <div class="top">
      <input type="text" v-model="input" placeholder="请输入渠道名称" />
      <div class="find" @click="finding()">查询</div>
      <div class="reset" @click="reseting()">重置</div>
    </div>
    <div class="manage">
      <div @click="add">添加渠道</div>
      <div @click="batchDelete">批量删除</div>
    </div>
    <el-table
      ref="multipleTable"
      :data="compData"
      tooltip-effect="dark"
      style="width: 100%"
      :header-cell-style="{ backgroundColor: '#EEEEEE' }"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="100"> </el-table-column>
      <el-table-column prop="name" label="渠道名称" width="200">
        <template slot-scope="scope">
          <input
            type="text"
            v-model="scope.row.name"
            v-show="scope.row.iseditor"
          />
          <span v-show="!scope.row.iseditor">{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="type" label="账号" width="200">
        <template slot-scope="scope">
          <input
            type="text"
            v-model="scope.row.type"
            v-show="scope.row.iseditor"
          />
          <span v-show="!scope.row.iseditor">{{ scope.row.type }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="color" label="状态" show-overflow-tooltip>
        <template slot-scope="scope">
          <input
            type="text"
            v-model="scope.row.color"
            v-show="scope.row.iseditor"
          />
          <span v-show="!scope.row.iseditor">{{
            scope.row.isuse ? "启用" : "禁用"
          }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            @click="handleEdit(scope.$index, scope.row)"
            type="text"
            size="small"
            >编辑
          </el-button>
          <el-button
            type="text"
            size="small"
            @click="issureuse(scope.row.id, scope.row)"
            >{{ scope.row.isuse ? "禁用" : "启用" }}</el-button
          >
          <el-button
            type="text"
            size="small"
            @click.native.prevent="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      style="width: 1000px; height: 1000px; position: fixed; left: 30%"
      title="用户信息"
      :visible.sync="zdydialog"
    >
      <el-form :model="form">
        <el-form-item label="渠道名称" width="80px">
          <el-input class="dialog_input" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="账号" width="80px">
          <el-input class="dialog_input" v-model="form.type"></el-input>
        </el-form-item>
        <!-- <el-form-item label="日期" width="80px">
                    <el-date-picker v-model="form.date" type="date" placeholder="选择日期">
                    </el-date-picker>
                </el-form-item> -->
        <el-form-item label="状态" width="80px">
          <el-input class="dialog_input" v-model="form.color"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage1"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="40"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getchannelList, postchannelList} from "@/api/index";
import axios from 'axios';
// import axios from "axios";
export default {
  name: "channelView",
  data() {
    return {
      currentPage: 1,
      pageSize: 5, //每页显示条数
      total: 0,
      currentPage1: 1,
      currentPage2: 5,
      currentPage3: 5,
      currentPage4: 4,
      tableData: [],
      multipleSelection: [],
      input: "",
      zdydialog: false,
      form: {},
      multipleSelectAry: [],
      submitType: "",
      batchDeleteArr: [],
    };
  },

  mounted() {
    this.getchannelList();
  },
  computed: {
    compData() {
      return this.tableData.slice(
        (this.currentPage - 1) * this.pageSize,
        this.currentPage * this.pageSize
      );
    },
  },
  methods: {
    // 获取数据
    async getchannelList() {
      const _data = await getchannelList();
      const _res = await _data.data;
      (this.tableData = _res), (this.total = this.tableData.length);
    },
    handleClick(row) {
      console.log(row);
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.currentPage = 1;
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 添加
    add() {
      this.form = {
        name: "",
        type: "",
        color: "",
        isuse: false,
        //是否禁用
        disabled: !this.isuse,
      };
      this.submitType = "add";
      this.zdydialog = true;
    },
    cancel() {
      this.zdydialog = false;
    },
    submit() {
      this.zdydialog = false;

      if (this.submitType == "add") {
        // this.form.date = reformat(this.form.date);
        // this.tableData.push(this.form);
        postchannelList(this.form).then(() => {
          this.getchannelList();
        });
      }
    },
    // 批量删除
    batchDelete() {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.multipleSelection.forEach((item) => {
            // console.log(item);
            this.tableData = this.tableData.filter((i) => {
              return i.id != item.id;
            });
          });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 编辑
    handleEdit(index, row) {
      console.log(index, row);
      // this.form = this.tableData[index]
      this.submitType = "update";
      this.form = row;
      this.zdydialog = true;
    },
    // 是否禁用
    issureuse(id, row) {
      console.log(id, row);
      row.isuse = !row.isuse;
      row.disabled=!row.isuse;
      axios.put(`http://localhost:3000/channelList/${id}`,row).then(res=>{
        console.log('修改成功',res.data)
       })
    },
    // 删除
    handleDelete(row) {
      console.log(row);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          axios({
            type:"delete",
            url:`http://localhost:3000/channelList/${row.id}`,
          })
          // this.tableData.splice(1);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 查询
    finding() {
      const input = this.input;
      // console.log(input);
      if (input) {
        // console.log("input输入的搜索内容：" + this.input)
        this.tableData = this.tableData.filter((data) => {
          return data.name.indexOf(input) > -1;
        });
      }
      // this.$refs.tableData.clearFilter()
    },
    // 重置
    reseting() {
      console.log(this.tableData);
    },
  },
};
</script>

<style lang="less" scoped>
.channelView {
  width: 1250px;
  height: 800px;
  margin-left: 10px;
  padding: 30px;
  background-color: #fff;
  line-height: 0;

  img {
    width: 70px;
    height: 50px;
  }

  .top {
    display: flex;
    align-items: center;

    input {
      width: 200px;
      height: 40px;
      border: 1px #dcdfe6 solid;
      box-sizing: border-box;
      padding: 0 15px;
      margin-right: 20px;
      border-radius: 4px;
      border: 1px solid #dcdfe6;
      outline-color: #409eff;
    }

    .find,
    .reset {
      width: 100px;
      height: 40px;
      background-color: #409eff;
      border-radius: 4px;
      margin-left: 20px;
      text-align: center;
      line-height: 40px;
      color: #fff;
    }

    .reset {
      background-color: #fff;
      color: #000;
      border: #999 solid 1px;
      box-sizing: border-box;
    }
  }

  .manage {
    width: 100%;
    height: 100px;
    display: flex;
    align-items: center;

    div {
      height: 18px;
      font-size: 18px;
      color: #409eff;
      line-height: 18px;
    }

    div:nth-child(1) {
      border-right: #409eff 2px solid;
      padding-right: 10px;
    }

    div:nth-child(2) {
      margin-left: 10px;
    }
  }

  .el-pagination {
    height: 60px;
    padding-top: 50px;
    text-align: right;
  }
}
</style>